package com.ph.irp.base.accountbook.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ph.irp.base.accountbook.dao.PBaseAccountbookDAO;
import com.ph.irp.base.accountbook.service.AccountbookService;
import com.ph.irp.base.accountbookType.dao.PBaseAccountbooktypeDAO;
import com.ph.irp.dto.AccountbookDTO;
import com.ph.irp.model.PBaseAccountbook;
import com.ph.irp.model.PBaseAccountbooktype;

public class AccountbookServiceImpl implements AccountbookService {

	// 会计科目DAO
	private PBaseAccountbookDAO accountbookDAOProxy;
	// 会计科目集合
	private List<PBaseAccountbook> pbAccountbookList = null;
	// 会计科目�����
	private PBaseAccountbook accountbookModel = null;

	// 会计科目类别DAO
	private PBaseAccountbooktypeDAO accountbookTypeDAOProxy = null;

	// pbaseAccountbooktype会计科目类别model
	private PBaseAccountbooktype pbaseAccountbooktype = null;

	// 会计科目前台页面dto集合
	private List<AccountbookDTO> accountbookList = null;

	// getAllAccountbook查询所有的会计科目
	@Override
	public List<PBaseAccountbook> getAllAccountbook()throws Throwable {
		// TODO Auto-generated method stub

		return accountbookDAOProxy.getAllAccountbooks();

	}

	/**
	 * 动态查询会计科目
	 */
	@Override
	public List<AccountbookDTO> getAccountbooks(
			PBaseAccountbook pbaseAccountbook) throws Throwable{
		// 创建Accountbook类型的数组列表
		accountbookList = new ArrayList<AccountbookDTO>();

		// 动态查询会计科目�����
		if (pbaseAccountbook == null) {
			pbAccountbookList = this.getAllAccountbook();
		} else {
			// 根据姓名和代码查询
			if (!pbaseAccountbook.getAccountbookName().equals("")
					&& !pbaseAccountbook.getAccountbookCode().equals("")) {
				pbAccountbookList = accountbookDAOProxy.getAccountbookByNameAndCode(
						pbaseAccountbook.getAccountbookName(), pbaseAccountbook
								.getAccountbookCode());
			}
			// 根据名称查询
			else if (!pbaseAccountbook.getAccountbookName().equals("")) {
				pbAccountbookList = accountbookDAOProxy
						.getAccountbookByName(pbaseAccountbook
								.getAccountbookName());
			}
			// 根据代码查询
			else if (!pbaseAccountbook.getAccountbookCode().equals("")) {
				pbAccountbookList = accountbookDAOProxy
						.getAccountbookByCode(pbaseAccountbook
								.getAccountbookCode());
			} else
				pbAccountbookList = this.getAllAccountbook();
		}
		// 如果查询得到的集合为空，直接返回
		if (pbAccountbookList.size() == 0)
			return null;
		// 如果查询得到的集合不为空，封装DTO组成新的list
		for (int i = 0; i < pbAccountbookList.size(); i++) {

			AccountbookDTO accountbook = new AccountbookDTO();

			pbaseAccountbook = pbAccountbookList.get(i);

			Integer typeId = Integer.valueOf((pbaseAccountbook
					.getAccountbookTypeId()));

			pbaseAccountbooktype = accountbookTypeDAOProxy
					.getAccountbooktype(typeId);

			accountbook.setAccountBook(pbaseAccountbook);

			accountbook.setBookType(pbaseAccountbooktype);

			accountbookList.add(accountbook);
		}

		return accountbookList;
	}

	/**
	 * 根据id来删除会计科目
	 */
	@Override
	public int deleteAccountbookById(Integer accountbookId) throws Throwable{
		// TODO Auto-generated method stub

		int reuslt = 0;

		reuslt = accountbookDAOProxy.deleteAccountbookById(accountbookId);

		return reuslt;
	}

	/**
	 * 新增会计科目
	 */
	@Override
	public int addAccountbook(PBaseAccountbook pbaseAccountbook)throws Throwable {
		// TODO Auto-generated method stub
		int result = 0;

		result = this.accountbookDAOProxy.addAccountbook(pbaseAccountbook);

		return result;
	}

	/**
	 * 更新会计科目
	 */
	@Override
	public int updateAccountbook(PBaseAccountbook pbaseAccountbook) throws Throwable{
		// TODO Auto-generated method stub
		int result = 0;

		result = this.accountbookDAOProxy.update(pbaseAccountbook);

		return result;
	}

	/**
	 * 根据id查询会计科目
	 */
	@Override
	public PBaseAccountbook getAccountbookById(int accountbookId)throws Throwable {
		// TODO Auto-generated method stub

		accountbookModel = this.accountbookDAOProxy
				.getAccountbookById(accountbookId);

		return accountbookModel;
	}

	public PBaseAccountbookDAO getAccountbookDAOProxy() {
		return accountbookDAOProxy;
	}

	public void setAccountbookDAOProxy(PBaseAccountbookDAO accountbookDAOProxy) {
		this.accountbookDAOProxy = accountbookDAOProxy;
	}

	public List<PBaseAccountbook> getPbAccountbookList() {
		return pbAccountbookList;
	}

	public void setPbAccountbookList(List<PBaseAccountbook> pbAccountbookList) {
		this.pbAccountbookList = pbAccountbookList;
	}

	public PBaseAccountbook getAccountbookModel() {
		return accountbookModel;
	}

	public void setAccountbookModel(PBaseAccountbook accountbookModel) {
		this.accountbookModel = accountbookModel;
	}

	

	/**
	 * @return the accountbookTypeDAOProxy
	 */
	public PBaseAccountbooktypeDAO getAccountbookTypeDAOProxy() {
		return accountbookTypeDAOProxy;
	}

	/**
	 * @param accountbookTypeDAOProxy the accountbookTypeDAOProxy to set
	 */
	public void setAccountbookTypeDAOProxy(
			PBaseAccountbooktypeDAO accountbookTypeDAOProxy) {
		this.accountbookTypeDAOProxy = accountbookTypeDAOProxy;
	}

	public PBaseAccountbooktype getPbaseAccountbooktype() {
		return pbaseAccountbooktype;
	}

	public void setPbaseAccountbooktype(PBaseAccountbooktype pbaseAccountbooktype) {
		this.pbaseAccountbooktype = pbaseAccountbooktype;
	}

	public List<AccountbookDTO> getAccountbookList() {
		return accountbookList;
	}

	public void setAccountbookList(List<AccountbookDTO> accountbookList) {
		this.accountbookList = accountbookList;
	}

	/**
	 * setters and getters for instance variables
	 * 
	 * @return
	 */
	


}
